from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import requests
import time
from bs4 import BeautifulSoup
import re
import json
import urllib
def getOnoCookies():

    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('0', ''),
    )
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    s = requests.session()
    s.get('http://portal.igpublish.com/iglibrary/search/advanceSearch', headers=headers,params=params,proxies = proxies)
    requests.cookies.RequestsCookieJar()  # 利用RequestsCookieJar获取cookies
    cookies = s.cookies.get_dict()  # 拿到cookies，首次是不需要的，本次拿到仅是为了以后下一页或改变排序方式时使用

    return cookies

def gettwoCookies(cookies):


    headers = {
        'Origin': 'http://portal.igpublish.com',
        'Accept-Encoding': 'gzip, deflate',
        'Wicket-Ajax-BaseURL': 'advanceSearch?0',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/xml, text/xml, */*; q=0.01',
        'Referer': 'http://portal.igpublish.com/iglibrary/search/advanceSearch?0',
        'X-Requested-With': 'XMLHttpRequest',
        'Proxy-Connection': 'keep-alive',
        'Wicket-Ajax': 'true',
    }

    params = (
        ('0-1.IBehaviorListener.0-pnlAdvanceSearchPanel-ctnPublisherFilters-frmFilter-pltFilter-recorder', ''),
    )

    data = {
        'pltFilter:recorder': '40288b8c2f8a9868012f8ac6bfa7000a'
    }
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    s = requests.session()
    response = s.post('http://portal.igpublish.com/iglibrary/search/advanceSearch', headers=headers,
                             params=params, cookies=cookies, data=data,proxies = proxies)
    requests.cookies.RequestsCookieJar()  # 利用RequestsCookieJar获取cookies
    s.cookies.get_dict()  # 拿到cookies，首次是不需要的，本次拿到仅是为了以后下一页或改变排序方式时使用



def gettwoSCookies(cookies):


    headers = {
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Accept': 'application/xml, text/xml, */*; q=0.01',
        'Referer': 'http://portal.igpublish.com/iglibrary/search/advanceSearch?0',
        'X-Requested-With': 'XMLHttpRequest',
        'Proxy-Connection': 'keep-alive',
        'Wicket-Ajax': 'true',
        'Wicket-Ajax-BaseURL': 'advanceSearch?0',
    }



    params = (
        ('0-1.IBehaviorListener.0-pnlAdvanceSearchPanel-ctnPublisherFilters-frmFilter-pltFilter', ''),
        ('_',int(time.time())),
    )
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    s = requests.session()
    response = requests.get('http://portal.igpublish.com/iglibrary/search/advanceSearch', headers=headers,
                            params=params, cookies=cookies,proxies = proxies)

    requests.cookies.RequestsCookieJar()  # 利用RequestsCookieJar获取cookies
    cookies = s.cookies.get_dict()  # 拿到cookies，首次是不需要的，本次拿到仅是为了以后下一页或改变排序方式时使用



def getThreeSCookies(keyword,cookies):


    headers = {
        'Origin': 'http://portal.igpublish.com',
        'Accept-Encoding': 'gzip, deflate',
        'Wicket-Ajax-BaseURL': 'advanceSearch?0',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/xml, text/xml, */*; q=0.01',
        'Referer': 'http://portal.igpublish.com/iglibrary/search/advanceSearch?0',
        'X-Requested-With': 'XMLHttpRequest',
        'Proxy-Connection': 'keep-alive',
        'Wicket-Ajax': 'true',
    }

    params = (
        ('0-1.IBehaviorListener.0-pnlAdvanceSearchPanel-ctnFields-frmSearch-viewFields-0-txtValue', ''),
    )

    data = {
        'viewFields:0:txtValue': keyword
    }
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    s = requests.session()
    response = s.post('http://portal.igpublish.com/iglibrary/search/advanceSearch', headers=headers,
                             params=params, cookies=cookies, data=data,proxies = proxies)

    requests.cookies.RequestsCookieJar()  # 利用RequestsCookieJar获取cookies
    cookies = s.cookies.get_dict()  # 拿到cookies，首次是不需要的，本次拿到仅是为了以后下一页或改变排序方式时使用


def getThreeCookies(keyword,cookies):
    headers = {
        'Origin': 'http://portal.igpublish.com',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'X-Requested-With': 'XMLHttpRequest',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Proxy-Connection': 'keep-alive',
        'Wicket-Ajax': 'true',
        'Wicket-FocusedElementId': 'id1c',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/xml, text/xml, */*; q=0.01',
        'Wicket-Ajax-BaseURL': 'advanceSearch?0',
        'Referer': 'http://portal.igpublish.com/iglibrary/search/advanceSearch?0',
    }

    params = (
        ('0-1.IBehaviorListener.1-pnlAdvanceSearchPanel-ctnFields-frmSearch-viewFields-0-txtValue', ''),
    )

    data = {
        'viewFields:0:txtValue': keyword,
        'keycode': ''
    }
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    s = requests.session()
    response = requests.post('http://portal.igpublish.com/iglibrary/search/advanceSearch', headers=headers,
                             params=params, cookies=cookies, data=data, proxies=proxies)
    requests.cookies.RequestsCookieJar()  # 利用RequestsCookieJar获取cookies
    cookies = s.cookies.get_dict()  # 拿到cookies，首次是不需要的，本次拿到仅是为了以后下一页或改变排序方式时使用

def getFourCookies(cookies):

    headers = {
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Wicket-FocusedElementId': 'id1b',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Accept': 'application/xml, text/xml, */*; q=0.01',
        'Referer': 'http://portal.igpublish.com/iglibrary/search/advanceSearch?0',
        'X-Requested-With': 'XMLHttpRequest',
        'Proxy-Connection': 'keep-alive',
        'Wicket-Ajax': 'true',
        'Wicket-Ajax-BaseURL': 'advanceSearch?0',
    }

    params = (
        ('0-1.IBehaviorListener.0-pnlAdvanceSearchPanel-lnkSearchBelow', ''),
        ('_', int(time.time())),
    )
    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    response = requests.get('http://portal.igpublish.com/iglibrary/search/advanceSearch', headers=headers,
                            params=params, cookies=cookies, proxies=proxies)

#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):


    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select(".briefmode_area")

    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li.select(".booktitle_m")[0].text.strip()
        except:
            pass
        try:
            tempData["titleHref"] = "http://portal.igpublish.com/iglibrary/search" + li.select(".booktitle_m")[0]["href"]
        except:
            pass

        try:
            tempData["author"] = [li.select(".bookauthor_m")[0].text.strip()]
        except:
            pass

        try:
            tempData["source"] = li.select(".bookpublisher_m")[0].text.strip()
        except:
            pass

        try:
            tempData["publishTime"] = li.select(".bookyear_m").text.strip()
        except:
            pass

        allData.append(tempData)

    return allData

def getData(cookies):


    headers = {
        'Proxy-Connection': 'keep-alive',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://portal.igpublish.com/iglibrary/search/advanceSearch?4',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    response = requests.get('http://portal.igpublish.com/iglibrary/search/main?1', headers=headers,cookies=cookies,proxies=proxies)


    allData = formData(response)


    return allData


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    cookies = getOnoCookies()

    gettwoCookies(cookies)
    gettwoSCookies(cookies)

    getThreeCookies(data["values"][0],cookies)
    getThreeSCookies(data["values"][0],cookies)

    getFourCookies(cookies)

    allData = getData(cookies)

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")